import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HolidayRequest {

    public static void main(String[] args) {
        ProcessEngineConfiguration cfg = getProcessEngineConfiguration();

        ProcessEngine processEngine = cfg.buildProcessEngine();

        ProcessDefinition processDefinition = deploy(processEngine);

        System.out.println("Found process definition : " + processDefinition.getName());

        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employee", "shouren");
        variables.put("nrOfHolidays", 3);
        variables.put("description", "我要去浪漫的土耳其");
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("holidayRequest", variables);

        System.out.println("processInstance");

        TaskService taskService = processEngine.getTaskService();
        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
        System.out.println("You have " + tasks.size() + " tasks:");
        for (int i=0; i<tasks.size(); i++) {
            System.out.println((i+1) + ") " + tasks.get(i).getName());
        }

        System.out.println("Which task would you like to complete?");

        Task task = tasks.get(0);
        Map<String, Object> processVariables = taskService.getVariables(task.getId());
        System.out.println(processVariables.get("employee") + " wants " +
                processVariables.get("nrOfHolidays") + " of holidays. Do you approve this?");

        System.out.println("------------approved--------------");

        variables.put("approved", true);
        taskService.complete(task.getId(), variables);
    }


    private static ProcessDefinition deploy(ProcessEngine processEngine) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday-request.bpmn20.xml")
                .deploy();


        return repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();
    }

    private static ProcessEngineConfiguration getProcessEngineConfiguration() {
        return new StandaloneProcessEngineConfiguration()
                    .setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1")
                    .setJdbcUsername("sa")
                    .setJdbcPassword("sa")
                    .setJdbcDriver("org.h2.Driver")
                    .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
    }
}
